package com.cake.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cake.base.util.ReturnUtils;
import com.cake.entity.XxShop;
import com.cake.service.inter.IXxShopService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 圈子控制层
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "shop")
public class ShopController {
	
	@Autowired
	private IXxShopService xxShopService;
	
	/**
	 * 查询所有的圈子
	 * @return
	 * @throws Exception 
	 */
	@PostMapping(value = "selectShops")
	public Object selectShops(@RequestBody XxShop xxShop) throws Exception{
		PageHelper.startPage(xxShop.getCurrentPage(), xxShop.getPageSize());
		List<XxShop> list = xxShopService.selectByAttributeAnd(new XxShop());
		for (XxShop xxShop2 : list) {
			Integer postType = xxShop2.getPostType();
            xxShop2.setPostTypeName(postType == 1 ?  "自营配送" : postType == 3 ? "快递" : "第三方闪送");
		}
		PageInfo<XxShop> page = new PageInfo<>(list);
		return ReturnUtils.forNoramlPage("000000", list, 
				page.getPageNum(), page.getPageSize(), page.getPages(), (int)page.getTotal());
	}
	
	/**
	 * 根据id查询一条圈子信息详情
	 * @return
	 * @throws Exception 
	 */
	@GetMapping(value = "selectShopById")
	public Object selectShopById(Long shopId) throws Exception{
		return ReturnUtils.forNoramlSuccess(xxShopService.selectByPrimaryKey(shopId));
	}
	
	/**
	 * 添加圈子
	 * @return
	 * @throws Exception 
	 */
	@PostMapping(value = "insertShop")
	public Object insertShop(@RequestBody XxShop xxShop) throws Exception{
		xxShop.setShopStatus(1);
		xxShop.setCreateDate(new Date());
		xxShop.setModifyDate(new Date());
		xxShopService.insertSelective(xxShop);
		return ReturnUtils.forNoraml("000000", "操作成功");
	}
	
	/**
	 * 修改一条圈子信息
	 * @return
	 * @throws Exception 
	 */
	@PostMapping(value = "updateShop")
	public Object updateShop(@RequestBody XxShop xxShop) throws Exception{
		xxShopService.updateByPrimaryKeySelective(xxShop);
		return ReturnUtils.forNoraml("000000", "操作成功");
	}
	
	/**
	 * 禁用一条圈子
	 * @return
	 * @throws Exception 
	 */
	@GetMapping(value = "forbiddenShop")
	public Object forbiddenShop(Long shopId) throws Exception{
		XxShop xxShop = xxShopService.selectByPrimaryKey(shopId);
		if(xxShop != null){
			xxShop.setOrders(1);
			xxShopService.updateByPrimaryKeySelective(xxShop);
		}
		return ReturnUtils.forNoraml("000000", "操作成功");
	}
	
}
